package com.team7.happycommunity.personcentor;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@SpringBootApplication
@MapperScan("com.team7.happycommunity.personcentor.dao")
@EnableCaching//开启声明式缓存
@EnableRedisHttpSession
@EnableEurekaClient
public class PersoncentorApplication {

	public static void main(String[] args) {
		SpringApplication.run(PersoncentorApplication.class, args);
	}

	@Bean
	public RedisCacheConfiguration redisCacheConfiguration(){
		RedisCacheConfiguration configuration =  RedisCacheConfiguration.defaultCacheConfig();
		configuration = configuration.serializeValuesWith(RedisSerializationContext.
				SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
		return configuration;
	}
	//RedisConnectionFactory对象是Spring工厂负责创建的，注入的过程中，Spring工厂
	//会根据bean的类型进行自动注入
	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory factory){
		RedisTemplate template = new RedisTemplate();
		template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		template.setKeySerializer(new StringRedisSerializer());
		template.setConnectionFactory(factory);
		return template;
	}


}
